<template>
<div>
  <!-- 面包屑导航 -->
  <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 20px;">
    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>音乐管理</el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/music/songList' }">歌单管理</el-breadcrumb-item>
    <el-breadcrumb-item>歌单分类</el-breadcrumb-item>
  </el-breadcrumb>
  <el-button size="mini" type="primary" icon="el-icon-plus" @click="handleAdd" class="add_btn">添加</el-button>
  <!--  列表数据  -->
  <el-table :data="list" border style="width: 100%" ref="multipleTable" >
    <el-table-column align="center" label="序号" width="60" type="index"></el-table-column>
    <el-table-column align="center" prop="name" label="分类名称" width="320"></el-table-column>
    <el-table-column align="center" prop="updateTime" label="更新时间" width="260">
      <template slot-scope="scope">
        <!-- 格式化日期 -->
        {{getFormat(scope.row.updateTime)}}
      </template>
    </el-table-column>
    <el-table-column align="center" prop="createTime" label="创建时间" width="260">
      <template slot-scope="scope">
        <!-- 格式化日期 -->
        {{getFormat(scope.row.createTime)}}
      </template>
    </el-table-column>
    <el-table-column  align="center" label="操作">
      <template slot-scope="scope">
        <el-button icon="el-icon-edit-outline" type="success" size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
        <el-button icon="el-icon-document-delete" type="danger" size="mini" @click="handleDel(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 添加或编辑弹框 -->
  <el-dialog :title="title" :visible.sync="visiable" width="30%" :before-close="handleClose">
    <el-form ref="formData" :model="formData" label-width="80px"  >
      <el-form-item label="分类名称:" prop="categoryName">
        <el-input v-model="formData.name" ></el-input>
      </el-form-item>
      <el-form-item align="right">
        <el-button type="primary" @click="submit('formData')">确认</el-button>
        <el-button type="danger" @click="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</div>
</template>

<script>
import {dateFormat} from "@/utils/date";
import musicApi from '@/api/music';
export default {
  data() {
    return {
      list: [],
      formData: {
        name: '',
        id: ''
      },
      title: '',
      visiable: false
    }
  },

  created() {
    this.fetchData();
  },

  methods: {
    // 获取歌单分类信息
    fetchData() {
      musicApi.getAllSongListCategory().then(response => {
        if (response.code === 200) {
          this.list = response.data;
        }
      })
    },

    // 删除
    handleDel(id) {
      this.$confirm('确认删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送删除请求
        musicApi.deleteSongListCategory(id).then(response => {
          if (response.code === 200) {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 1500
            });
          } else if (response.code === 306) {
            this.$notify.error({
              title: '错误',
              message: '您没有操作权限，请联系管理员',
              duration: 1500
            });
          } else {
            this.$notify.error({
              title: '错误',
              message: '系统异常，请稍后再试',
              duration: 1500
            });
          }
          // 刷新列表
          this.fetchData();
        })
      }).catch(() => {
        // 取消删除
      });
    },

    // 编辑
    handleEdit(id) {
      // 根据歌单分类id查询歌单分类详情
      musicApi.getSongListCategoryById(id).then(response => {
        if (response.code === 200) {
          this.formData = response.data;
          this.title = '编辑';
          this.visiable = true;
        }
      })
    },

    // 提交
    submit() {
      if (this.formData.name.length === 0) {
        this.$notify.error({
          title: '错误',
          message: '歌单分类名称不能为空',
          duration: 1500
        });
        this.handleClose();
      }
      if (this.title === '编辑') {
        // 编辑
        musicApi.updateSongListCategory(this.formData).then(response => {
          if (response.code === 200) {
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 1500
            });
          } else if (response.code === 306) {
            this.$notify.error({
              title: '错误',
              message: '您没有操作权限，请联系管理员',
              duration: 1500
            });
          } else {
            this.$notify.error({
              title: '错误',
              message: '系统异常，请稍后再试',
              duration: 1500
            });
          }
          // 刷新列表
          this.fetchData();
          this.handleClose();
        })
      } else {
        // 添加
        musicApi.saveSongListCategory(this.formData).then(response => {
          if (response.code === 200) {
            this.$notify({
              title: '成功',
              message: '添加成功',
              type: 'success',
              duration: 1500
            });
          } else if (response.code === 306) {
            this.$notify.error({
              title: '错误',
              message: '您没有操作权限，请联系管理员',
              duration: 1500
            });
          } else {
            this.$notify.error({
              title: '错误',
              message: '系统异常，请稍后再试',
              duration: 1500
            });
          }
          // 刷新列表
          this.fetchData();
          this.handleClose();
        })
      }
    },

    // 添加
    handleAdd() {
      this.title = '添加';
      this.visiable = true;
    },

    // 关闭弹框
    handleClose() {
      this.title = '';
      this.visiable = false;
      this.formData.id = '';
      this.formData.name = '';
    },

    // 格式化日期
    getFormat(date){
      return dateFormat(date);
    },
  }
}
</script>

<style scoped>
.add_btn {
  margin-bottom: 10px;
}
</style>
